<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta name="author" content="" />
<link href="../../css/main.css" rel="stylesheet" />
<script type="text/javascript" src="../../js/prototype.js"></script>
<script type="text/javascript" src="../../js/scriptaculous.js"></script>
<script type="text/javascript" src="../../js/controls.js"></script>
<script type="text/javascript" src="../../js/page.js"></script>
<script type="text/javascript">
function upd_cdc(classe, materia, sel){
    var doc = sel.value;
    if(doc == 0){
        alert("Docente non selezionato");
        return;
    }
    var url = "<?php print $_SESSION['__config__']['root_site'] ?>admin/adm_classes/update_cdc.php";
    //var data = "cls="+classe+"&mat="+materia+"&doc="+doc;
    var req = new Ajax.Request(url,
			  {
			    	method:'post',
			    	parameters: {cls: classe, mat: materia, doc: doc},
			    	onSuccess: function(transport){
			    		var response = transport.responseText || "no response text";
			    		//alert(response);
			    		dati = response.split(";");
			    		if(dati[0] != "ko"){
							//alert(response);
			            }
			            else{
			                alert("Aggiornamento non riuscito. Query: "+dati[1]+"\nErrore: "+dati[2]);
			                return;
			            }
			    	},
			    	onFailure: function(){ alert("Si e' verificato un errore...") }
			  });
}
</script>
<style type="text/css">TD {height: 11px}</style>
</head>
<body <?php if(isset($msg)) print("onLoad=\"alert('".$msg."'); window.close(); window.opener.document.location.href='users.php'\"") ?>>
    <p style="text-align: center; font-size: 12px; font-weight: bold">Gestione Consiglio di classe: <?php print $classe['anno_corso'].$classe['sezione'] ?></p>
    <form action="cdc.php?upd=1" method="post">
    <div style="text-align: left">
    <table style="width: 420px; margin: auto">
    <?php
    while($mat = $res_mat->fetch_assoc()){
        if($mat['id_materia'] == 1 || $mat['id_materia'] == 2 || $mat['id_materia'] == 1006)
            $mt = 12;
        else
            $mt = $mat['id_materia'];
        $sel_doc = "SELECT uid, nome, cognome FROM utenti, docenti WHERE uid = id_docente AND materia = $mt ORDER BY cognome, nome";
        //print $sel_doc;
        try{
        	$res_doc = $db->executeQuery($sel_doc);
        } catch (MySQLException $ex){
        	$ex->alert();
        }
    ?>
        <tr>
            <td class="popup_title" style="width: 230px; color: #003366; padding-top: 1px; padding-bottom: 1px; font-weight: bold"><?php print $mat['materia'] ?></td>
            <td style="width: 190px; padding-top: 3px; padding-bottom: 3px">
                <select onchange="upd_cdc(<?php print $classID ?>, <?php print $mat['id_materia'] ?>, this)" name="sel<?php print $mat['id_materia'] ?>" style="width: 180px; font-size: 11px; color: #036">
                    <option value="0">Nessuno</option>
    <?php
        while($dc = $res_doc->fetch_assoc()){
            if($dc['uid'] == $consiglio[$mat['id_materia']]){
    ?>
                    <option value="<?php print $dc['uid'] ?>" selected="selected"><?php print $dc['cognome']." ".$dc['nome'] ?></option>
    <?php
            }
            else{
    ?>
                    <option value="<?php print $dc['uid'] ?>"><?php print $dc['cognome']." ".$dc['nome'] ?></option>
    <?php
            }
        }
    ?>
                </select>
            </td>
        </tr>
        <?php 
    	} 
    	if($classe['sezione'] == "C"){
    		$sel_strumenti = "SELECT * FROM materie WHERE idpadre = 13";
    		$res_strumenti = $db->executeQuery($sel_strumenti);
    		$sel_doc_str = "SELECT uid, nome, cognome FROM utenti, docenti WHERE uid = id_docente AND materia IN (13, 21, 22, 23, 24) ORDER BY cognome, nome";
    		$res_doc_str = $db->executeQuery($sel_doc_str);
    		$doc_str = array();
    		while($d_s = $res_doc_str->fetch_assoc()){
    			array_push($doc_str, $d_s);
    		}
    		while($str = $res_strumenti->fetch_assoc()){
    	?>
    	<tr>
            <td class="popup_title" style="width: 230px; color: #003366; padding-top: 1px; padding-bottom: 1px; font-weight: bold"><?php print $str['materia'] ?></td>
            <td style="width: 190px; padding-top: 5px; padding-bottom: 5px">
                <select onchange="upd_cdc(<?php print $classID ?>, <?php print $str['id_materia'] ?>, this)" name="sel<?php print $str['id_materia'] ?>" style="width: 180px; font-size: 11px; color: #036">
                    <option value="0">Nessuno</option>
        <?php
        		reset($doc_str);
        		foreach($doc_str as $ds){
            		if($ds['uid'] == $consiglio[$str['id_materia']]){
    ?>
                    <option value="<?php print $ds['uid'] ?>" selected="selected"><?php print $ds['cognome']." ".$ds['nome'] ?></option>
    <?php
            		}
            		else{
    ?>
                    <option value="<?php print $ds['uid'] ?>"><?php print $ds['cognome']." ".$ds['nome'] ?></option>
    <?php
            		}
        		}
    ?>
                </select>
            </td>
        </tr>
        <?php 
    		}
    	}
        ?>
        <tr>
            <td class="popup_title" style="width: 230px; color: #003366; padding-top: 1px; padding-bottom: 1px; font-weight: bold">Coordinatore</td>
            <td style="width: 190px; padding-top: 5px; padding-bottom: 5px">
                <select onchange="upd_cdc(<?php print $classID ?>, 'coord', this)" name="coord" style="width: 180px; font-size: 11px; color: #036">
                    <option value="0">Nessuno</option>
    <?php
    	$sel_coord = "SELECT uid, nome, cognome, coordinatore FROM utenti, docenti, cdc WHERE uid = docenti.id_docente AND docenti.id_docente = cdc.id_docente AND cdc.id_classe = $classID AND cdc.id_anno = $anno GROUP BY uid, nome, cognome, coordinatore ORDER BY cognome, nome";
        //print $sel_coord;
        $res_coord = $db->execute($sel_coord);
        while($dc = $res_coord->fetch_assoc()){
        	if($dc['coordinatore'] == 1){
    ?>
                    <option value="<?php print $dc['uid'] ?>" selected="selected"><?php print $dc['cognome']." ".$dc['nome'] ?></option>
    <?php
        	}
    		else{
    ?>
    				<option value="<?php print $dc['uid'] ?>"><?php print $dc['cognome']." ".$dc['nome'] ?></option>
    <?php 		}
            }
    ?>
                </select>
            </td>
        </tr>
        <tr>
            <td colspan="2" style="height: 15px">&nbsp;&nbsp;&nbsp;</td>
        </tr>
    </table>
    <div style="margin-right: 10px; width: 410px; text-align: right">
        <a href="#" onclick="parent.win.close(); parent.location.href = 'classi.php?second=1&offset=<?php print $_REQUEST['offset'] ?>'">Chiudi</a>
    </div>
    </div>
   </form>
</body>
</html>